package thread;

import util.ThreadUtil;

/**
 * @author: omybug
 * @date: 18/11/19 10:45
 */
public class JoinTest {
    class MyThread extends Thread{
        @Override
        public void run() {
            for (int i = 0; i < 5; i++) {
                // 为了更容易体现效果，然线程sleep一下。
                try {
                    Thread.sleep(1L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程1第" + i + "次执行！");
            }
        }
    }

    public JoinTest(){
        MyThread myThread = new MyThread();
        myThread.start();
        for (int i = 0; i < 5; i++) {
            System.out.println("主线程第" + i +"次执行！");
            if (i >= 2) try {
                //t1线程合并到主线程中，主线程停止执行过程，转而执行t1线程，直到t1执行完毕后继续。
                myThread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("--- end ---");
    }

    public static void main(String[] args) {
//        int a;
//        boolean b = ( a = 1) == 0;
//        System.out.println(a);
//        System.out.println(b);
        JoinTest joinTest = new JoinTest();
    }
}
